//
//  LeDiscovery.h
//
//
//  Created by  on 2019/2/18.
//  Copyright © 2019 . All rights reserved.
//  蓝牙扫描连接

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "SROTADefines.h"

#import "CABLeService.h"

#define nDidDisconnect @"DidDisconnect"

@class CABleDiscovery;

/****************************************************************************/
/*							UI protocols									*/
/***************************************************************************/
@protocol STLeDiscoveryDelegate <NSObject>
@required
/// 扫描刷新 清除所有发现设备,连接过的设备, 断开已连接设备
- (void)stdiscoveryDidRefresh:(NSArray<CABLeService *> *)perphelArray;

/// ble state call back
/// @param isOn YES = statePowerOn , NO = statePowerOff
- (void)stdiscoveryStatePoweredState:(CBManagerState)state;
- (void)stconnectPeripheralTimeout:(CABLeService*)keyService;//连接超时方法
- (void)stdidRetrievePeripherals:(NSArray *)peripherals;//恢复设备

@optional
-(void)stabnormalDisconnect:(CABleDiscovery *)service;
@end



/****************************************************************************/
/*							Discovery class									*/
/****************************************************************************/

typedef NS_ENUM(NSUInteger, LEDISCOVERY_SCANTYPE) {
    LEDISCOVERY_SCANTYPE_AUTOCONNECT,  // 连接第一个扫描的设备
    LEDISCOVERY_SCANTYPE_RECONECT,      // 用户单击按钮连接
    LEDISCOVERY_SCANTYPE_LISTCHOOSE,    // 用户从列表选择连接
};

@interface CABleDiscovery : NSObject

@property (nonatomic, weak) id<STLeDiscoveryDelegate>       discoveryDelegate;
@property (nonatomic, weak) id<StLeKeyFobProtocol>          peripheralDelegate;

@property (retain, nonatomic) NSMutableArray<CABLeService *>    *foundPeripherals;//发现设备
@property(strong, atomic, readonly)CABLeService *currentService; //当前正在处理中的外设

@property(strong, nonatomic) NSArray<NSArray<NSNumber *> * > *cidArray; //新的广播包解析辨识

+ (instancetype) sharedInstance;//初始化
+ (CBUUID*) sigUUIDToCBUUID:(uint16_t)UUID;
+ (uint16_t) sigUUIDFromCBUUID:(CBUUID*)UUID;

-(void)ousideSetCurrentService:(CABLeService * _Nullable)service;

-(void)regist;
/*!
 *  @method sendValue:(CBPeripheral*)peripheral sendData:(NSData *)data type:(CBCharacteristicWriteType)type
 *
 *  @param peripheral   A <code>CBPeripheral</code>.
 *  @param data                The value to write.
 *  @param type                The type of write to be executed.
 *
 *  @discussion                Writes <i>value</i> to <i>CBPeripheral</i>.
 *
 */
- (void)sendData:(NSData *)data type:(CBCharacteristicWriteType)type;

- (CBManagerState)deviceBleCenterState;

- (void)startScanning:(LEDISCOVERY_SCANTYPE)scanType
            ScanOption:(NSDictionary * _Nullable)options;//开始扫描
- (void) stopScanning;//停止扫描
- (void)cancelAllReconnect;//取消重连

- (void) connectPeripheral:(CABLeService*)keyService;//连接外设 用于用户从列表选择设备
- (void) disconnectPeripheral:(CABLeService*)keyService;//手动断开外设

- (void) retrievePeripheral:(NSString *)uuid;//重连外设



@property (nonatomic) BOOL isRefreahing;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;


@end
